package com.lfs.admin.controller;

import com.lfs.admin.model.dto.ShopDto;
import com.lfs.admin.model.entity.ShopEntity;
import com.lfs.admin.model.vo.ShopVo;
import com.lfs.admin.service.ShopService;
import com.lfs.base.dto.Result;
import com.lfs.base.dto.ResultObject;
import com.lfs.base.exception.BusinessException;
import com.lfs.common.annotation.Log;
import com.lfs.common.core.controller.BaseController;
import com.lfs.common.core.page.TableDataInfo;
import com.lfs.common.enums.BusinessType;
import com.lfs.dao.entity.PageBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/shop")
@Slf4j
@Api(value = "店铺管理", tags = {"店铺管理"})
public class ShopController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(ShopController.class);

    @Autowired
    private ShopService shopService;

    @Log(title = "查询店铺列表", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('shop:list')")
    @ApiOperation(value = "查询店铺列表")
    @PostMapping("/list")
    public Result<?> list(@RequestBody ShopDto shopDto) {
        if (null == shopDto) {
            throw new BusinessException("查询店铺列表接口参数不能为空!");
        }

        logger.info("根据{},查询店铺列表请求参数", shopDto.toString());
        PageBean<ShopEntity> shopEntityList = shopService.queryShopList(shopDto);
        return ResultObject.successObject(shopEntityList,"success");
    }

    @Log(title = "新增店铺", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('shop:add')")
    @ApiOperation(value = "新增店铺")
    @PostMapping("/addShopInfo")
    public Result<?> addShopInfo(@RequestBody ShopDto shopDto) {
        if (null == shopDto) {
            throw new BusinessException("查询店铺列表接口参数不能为空!");
        }
        logger.info("根据{},查询店铺列表请求参数", shopDto.toString());
        return ResultObject.successObject(shopService.addShopInfo(shopDto),"success");
    }

    @Log(title = "查询已过期或即将过期的店铺列表", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('shop:list')")
    @ApiOperation(value = "查询已过期或即将过期的店铺列表")
    @PostMapping("/queryExpireShopList")
    public Result<?> queryExpireShopList() {
        List<ShopEntity> shopEntityList = shopService.queryExpireShopList();
        return ResultObject.successObject(shopEntityList,"success");
    }

    @Log(title = "查询当前用户下可用的店铺列表", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('shop:list')")
    @ApiOperation(value = "查询当前用户下可用的店铺列表")
    @PostMapping("/queryUserShopList")
    public Result<?> queryUserShopList() {
        logger.info("查询当前用户下可用的店铺列表");
        List<ShopEntity> shopEntityList = shopService.queryUserShopList();
        return ResultObject.successObject(shopEntityList,"success");
    }

    @Log(title = "生成随机店铺id", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('shop:list')")
    @ApiOperation(value = "生成随机店铺id")
    @GetMapping("/generateShopId")
    public Result<?> generateShopId(){
        String shopId = UUID.randomUUID().toString().substring(0,8);
        return ResultObject.successObject(shopId,"生成成功!");
    }


    @ApiOperation(value = "删除店铺")
    @DeleteMapping("/delete")
    public Result<?> deleteShop(@RequestParam("id") Integer id) {

        if (null == id) {
            throw new BusinessException("删除店铺接口参数不能为空!");
        }
        logger.info("根据{},删除店铺请求参数", id);
        return ResultObject.successObject(shopService.deleteShop(id), "删除店铺成功");
    }
}
